package com.qf.driver.api;

import com.qf.common.bean.R;
import com.qf.common.dto.CmdParamDto;
import com.qf.driver.service.CommandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 发送指令给emqx服务器
 * @author 千锋健哥
 */
@RestController
@RequestMapping("/driver/cmd")
public class CmdController {

    @Autowired
    private CommandService commandService;

    /**
     * 指令发送方法
     * @return
     */
    @PostMapping("/send")
    public R<String> send(@RequestBody(required = true) CmdParamDto dto) {
        //1. 判断dto中, 指令名称, 指令值, 设备key不能为空
        if (StringUtils.isEmpty(dto.getDeviceKey())
                || StringUtils.isEmpty(dto.getCmdName())
                || StringUtils.isEmpty(dto.getValue())) {
            return R.fail("设备key, 指令名称, 指令值都不可以为空!");
        }
        //2. 调用service, 发送指令
        Boolean flag = commandService.send(dto);

        //3. 判断发送结果是否成功返回
        if (flag) {
            return R.ok("发送成功!");
        } else {
            return R.fail("发送失败!");
        }
    }
}
